import numpy as np
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from scipy.spatial.distance import cdist

# 1. 生成高斯向量数据
def generate_high_dim_vector(num_points=1000, dimension=50):
    """生成随机高维数据"""
    np.random.seed(42)
    data = np.random.randn(num_points, dimension)
    return data

high_dim_vectors = generate_high_dim_vector(num_points=1000, dimension=50)

# 2. PCA 降维
def apply_pca(data, n_components):
    """使用 PCA 进行降维"""
    pca = PCA(n_components=n_components)
    reduced_data = pca.fit_transform(data)
    return reduced_data

pca_reduced = apply_pca(high_dim_vectors, n_components=10)

# 3. t-SNE 降维
def apply_tsne(data, n_components=2, perplexity=30, n_iter=1000):
    """使用 t-SNE 进行降维"""
    tsne = TSNE(n_components=n_components, perplexity=perplexity, n_iter=n_iter, random_state=42)
    reduced_data = tsne.fit_transform(data)
    return reduced_data

tsne_reduced = apply_tsne(high_dim_vectors)

# 4. 语义保留测量
def semantic_preservation_analysis(original_data, reduced_data):
    """计算降维前后数据点相似度的变化"""
    original_distances = cdist(original_data, original_data, metric='euclidean')
    reduced_distances = cdist(reduced_data, reduced_data, metric='euclidean')
    correlation = np.corrcoef(original_distances.flatten(), reduced_distances.flatten())[0, 1]
    return correlation

pca_correlation = semantic_preservation_analysis(high_dim_vectors, pca_reduced)
tsne_correlation = semantic_preservation_analysis(high_dim_vectors, tsne_reduced)

# 5. 检索精度评价
def retrieval_performance(query_vector, data):
    """模拟大尺度检索"""
    distances = cdist(query_vector, data, metric='euclidean').flatten()
    top_k_indices = np.argsort(distances)[:10]  # 检索最近的10个点
    top_k_distances = distances[top_k_indices]
    return top_k_indices, top_k_distances

query_vector = np.random.randn(1, 50)  # 模拟一个查询向量

original_indices, original_distances = retrieval_performance(query_vector, high_dim_vectors)
pca_indices, pca_distances = retrieval_performance(query_vector[:, :10], pca_reduced)
tsne_indices, tsne_distances = retrieval_performance(query_vector[:, :2], tsne_reduced)

# 输出结果
print("PCA 保留的语义相关性:", pca_correlation)
print("t-SNE 保留的语义相关性:", tsne_correlation)

print("\n原始数据检索最近10个点的距离:", original_distances)
print("PCA 降维后检索最近10个点的距离:", pca_distances)
print("t-SNE 降维后检索最近10个点的距离:", tsne_distances)